home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ov143b.zip / GETOPT.C < prev    next >
C/C++ Source or Header  |  1993-01-04  |  5KB  |  144 lines

  1. /*
  2.  
  3. GETOPT                                                  GETOPT
  4.  
  5.  
  6. NAME
  7.         getopt - get option letter from argv
  8.  
  9. SYNOPSIS
  10.         int getopt(argc, argv, optstring)
  11.         int argc;
  12.         char **argv;
  13.         char *optstring;
  14.  
  15.         extern char *optarg;
  16.         extern int optind, opterr;
  17.  
  18. DESCRIPTION
  19.         Getopt returns the next option letter in argv that matches a letter
  20.         in optstring.  Optstring is a string of recognized option letters;
  21.         if a letter is followed by a colon, the option is expected to have
  22.         an argument that may or may not be seperated from it by white space.
  23.         Optarg is set to point to the start of the option argument on
  24.         return from getopt.
  25.  
  26.         Getopt places in optind the argv index of the next argument to be
  27.         processed.  Because optind is external, it is normally initialized
  28.         to zero automatically before the first call to getopt.
  29.  
  30.         When all options have been processed (i.e. up to the first non
  31.         option argument), getopt returns EOF.  The special option -- may
  32.         be used to delimit the end of the options;  EOF will be returned
  33.         and -- will be skipped.
  34.  
  35. DIAGNOSTICS
  36.         Getopt prints an error message on stderr and returns a question
  37.         mark (?) when it encounters an option letter not included in
  38.         optstring.
  39.  
  40. EXAMPLE
  41.         The following code fragment shows how one might process the arg-
  42.         uments for a command that can take the mutually exclusive options
  43.         a and b, and the options f and o, both of which require arguments:
  44.  
  45.         main(argc,argv)
  46.         int argc;
  47.         char **argv;
  48.         {
  49.                 int c;
  50.                 extern char *optarg;
  51.                 extern int optind;
  52.                 .
  53.                 .
  54.                 .
  55.                 while ((c = getopt(argc,argv,"abf:o:")) != EOF)
  56.                         switch (c) {
  57.                         case 'a':
  58.                                 if (bflg)
  59.                                         errflg++;
  60.                                 else
  61.                                         aflg++;
  62.                                 break;
  63.                         case 'b':
  64.                                 if (aflg)
  65.                                         errflg++;
  66.                                 else
  67.                                         bflg++;
  68.                                 break;
  69.                         case 'f':
  70.                                 ifile = optarg;
  71.                                 break;
  72.                         case 'o':
  73.                                 ofile = optarg;
  74.                                 break;
  75.                         case '?':
  76.                         default:
  77.                                 errflg++;
  78.                                 break;
  79.                         }
  80.                 if (errflg) {
  81.                         fprintf(stderr,"Usage...");
  82.                         exit(2);
  83.                 }
  84.  
  85.                 for ( ; optind < argc; optind++) {
  86.                         if (argv[optind]...
  87.                         .
  88.                         .
  89.                         .
  90. }
  91. ******************************************************************************/
  92.  
  93. #include <stdio.h>
  94.  
  95. /*
  96.  * get option letter from argument vector
  97.  */
  98. int     opterr = 1,             /* useless, never set or used */
  99.         optind = 1,             /* index into parent argv vector */
  100.         optopt;                 /* character checked for validity */
  101. char    *optarg;                /* argument associated with option */
  102.  
  103. #define BADCH   (int)'?'
  104. #define EMSG    ""
  105. #define tell(s) fputs(*nargv,stderr);fputs(s,stderr); \
  106.                 fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
  107.  
  108. getopt(nargc,nargv,ostr)
  109. int     nargc;
  110. char    **nargv,
  111.         *ostr;
  112. {
  113.         static char     *place = EMSG;  /* option letter processing */
  114.         register char   *oli;           /* option letter list index */
  115.         char    *strchr();
  116.  
  117.         if(!*place) {                   /* update scanning pointer */
  118.                 if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
  119.                 if (*place == '-') {    /* found "--" */
  120.                         ++optind;
  121.                         return(EOF);
  122.                 }
  123.         }                               /* option letter okay? */
  124.         if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
  125.                 if(!*place) ++optind;
  126.                 tell(": illegal option -- ");
  127.         }
  128.         if (*++oli != ':') {            /* don't need argument */
  129.                 optarg = NULL;
  130.                 if (!*place) ++optind;
  131.         }
  132.         else {                          /* need an argument */
  133.                 if (*place) optarg = place;     /* no white space */
  134.                 else if (nargc <= ++optind) {   /* no arg */
  135.                         place = EMSG;
  136.                         tell(": option requires an argument -- ");
  137.                 }
  138.                 else optarg = nargv[optind];    /* white space */
  139.                 place = EMSG;
  140.                 ++optind;
  141.         }
  142.         return(optopt);                 /* dump back option letter */
  143. }
  144.